<template>
  <div>
    <div class="user-info-head" @click="editCropper()">
      <img v-bind:src="options.img" title="点击上传头像" class="img-circle" />
      <div class="avatar-hover">
        <i class="el-icon-camera"></i>
      </div>
    </div>
    
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body @opened="modalOpened" @close="closeDialog" custom-class="avatar-dialog">
      <div class="dialog-header-decoration"></div>
      
      <el-row>
        <el-col :xs="24" :md="12" :style="{height: '350px'}">
          <div class="cropper-container">
            <vue-cropper
              ref="cropper"
              :img="options.img"
              :info="true"
              :autoCrop="options.autoCrop"
              :autoCropWidth="options.autoCropWidth"
              :autoCropHeight="options.autoCropHeight"
              :fixedBox="options.fixedBox"
              :outputType="options.outputType"
              @realTime="realTime"
              v-if="visible"
            />
          </div>
        </el-col>
        <el-col :xs="24" :md="12" :style="{height: '350px'}">
          <div class="avatar-upload-preview">
            <img :src="previews.url" :style="previews.img" />
            <div class="preview-rail"></div>
          </div>
        </el-col>
      </el-row>
      
      <br />
      
      <el-row class="cropper-control-row">
        <el-col :lg="2" :sm="3" :xs="4">
          <el-upload action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
            <el-button size="small" class="control-btn select-btn">
              <i class="el-icon-upload"></i> 选择
            </el-button>
          </el-upload>
        </el-col>
        <el-col :lg="{span: 1, offset: 2}" :sm="2" :xs="3">
          <el-button icon="el-icon-plus" size="small" @click="changeScale(1)" class="control-btn"></el-button>
        </el-col>
        <el-col :lg="{span: 1, offset: 1}" :sm="2" :xs="3">
          <el-button icon="el-icon-minus" size="small" @click="changeScale(-1)" class="control-btn"></el-button>
        </el-col>
        <el-col :lg="{span: 1, offset: 1}" :sm="2" :xs="3">
          <el-button icon="el-icon-refresh-left" size="small" @click="rotateLeft()" class="control-btn"></el-button>
        </el-col>
        <el-col :lg="{span: 1, offset: 1}" :sm="2" :xs="3">
          <el-button icon="el-icon-refresh-right" size="small" @click="rotateRight()" class="control-btn"></el-button>
        </el-col>
        <el-col :lg="{span: 2, offset: 6}" :sm="2" :xs="4">
          <el-button type="primary" size="small" @click="uploadImg()" class="submit-btn">
            <i class="el-icon-check"></i> 提交
          </el-button>
        </el-col>
      </el-row>
      
      <div class="dialog-track">
        <div class="track-line"></div>
        <div class="track-train"></div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import store from "@/store";
import { VueCropper } from "vue-cropper";
import { uploadAvatar } from "@/api/system/user";
import { debounce } from '@/utils'

export default {
  components: { VueCropper },
  data() {
    return {
      // 是否显示弹出层
      open: false,
      // 是否显示cropper
      visible: false,
      // 弹出层标题
      title: "修改头像",
      options: {
        img: store.getters.avatar,  //裁剪图片的地址
        autoCrop: true,             // 是否默认生成截图框
        autoCropWidth: 200,         // 默认生成截图框宽度
        autoCropHeight: 200,        // 默认生成截图框高度
        fixedBox: true,             // 固定截图框大小 不允许改变
        outputType:"png",           // 默认生成截图为PNG格式
        filename: 'avatar'          // 文件名称
      },
      previews: {},
      resizeHandler: null
    };
  },
  methods: {
    // 编辑头像
    editCropper() {
      this.open = true;
    },
    // 打开弹出层结束时的回调
    modalOpened() {
      this.visible = true;
      if (!this.resizeHandler) {
        this.resizeHandler = debounce(() => {
          this.refresh()
        }, 100)
      }
      window.addEventListener("resize", this.resizeHandler)
    },
    // 刷新组件
    refresh() {
      this.$refs.cropper.refresh();
    },
    // 覆盖默认的上传行为
    requestUpload() {
    },
    // 向左旋转
    rotateLeft() {
      this.$refs.cropper.rotateLeft();
    },
    // 向右旋转
    rotateRight() {
      this.$refs.cropper.rotateRight();
    },
    // 图片缩放
    changeScale(num) {
      num = num || 1;
      this.$refs.cropper.changeScale(num);
    },
    // 上传预处理
    beforeUpload(file) {
      if (file.type.indexOf("image/") == -1) {
        this.$modal.msgError("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
      } else {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => {
          this.options.img = reader.result;
          this.options.filename = file.name;
        };
      }
    },
    // 上传图片
    uploadImg() {
      this.$refs.cropper.getCropBlob(data => {
        let formData = new FormData();
        formData.append("avatarfile", data, this.options.filename);
        uploadAvatar(formData).then(response => {
          this.open = false;
          this.options.img = process.env.VUE_APP_BASE_API + response.imgUrl;
          store.commit('SET_AVATAR', this.options.img);
          this.$modal.msgSuccess("修改成功");
          this.visible = false;
        });
      });
    },
    // 实时预览
    realTime(data) {
      this.previews = data;
    },
    // 关闭窗口
    closeDialog() {
      this.options.img = store.getters.avatar
      this.visible = false;
      window.removeEventListener("resize", this.resizeHandler)
    }
  }
};
</script>

<style scoped lang="scss">
.user-info-head {
  position: relative;
  margin: 0 auto;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  overflow: hidden;
  z-index: 3;
  
  &:hover .avatar-hover {
    opacity: 1;
  }
  
  .img-circle {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: all 0.3s;
  }
  
  .avatar-hover {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.4);
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
    transition: all 0.3s;
    
    i {
      color: #fff;
      font-size: 24px;
    }
  }
}

.avatar-dialog {
  border-radius: 12px;
  overflow: hidden;
  
  ::v-deep .el-dialog__header {
    padding: 15px 20px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    position: relative;
    background: linear-gradient(to right, #f8f9fa, #ffffff);
    
    .el-dialog__title {
      font-size: 18px;
      font-weight: 600;
      color: #3a5e82;
    }
  }
  
  .dialog-header-decoration {
    position: absolute;
    top: 0;
    right: 0;
    width: 80px;
    height: 5px;
    background: linear-gradient(90deg, transparent, #3a5e82);
    z-index: 10;
  }
  
  ::v-deep .el-dialog__body {
    padding: 20px;
  }
}

.cropper-container {
  height: 100%;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #e4e7ed;
  background-color: #f5f7fa;
}

.avatar-upload-preview {
  position: relative;
  width: 200px;
  height: 200px;
  border-radius: 50%;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  margin: 50px auto;
  border: 4px solid #ffffff;
  
  .preview-rail {
    position: absolute;
    bottom: -10px;
    left: 50%;
    transform: translateX(-50%);
    width: 150%;
    height: 4px;
    background: linear-gradient(90deg, 
      transparent, transparent,
      rgba(58, 94, 130, 0.3) 10%, rgba(58, 94, 130, 0.3) 20%,
      transparent 20%, transparent 30%,
      rgba(58, 94, 130, 0.3) 30%, rgba(58, 94, 130, 0.3) 40%,
      transparent 40%, transparent 50%,
      rgba(58, 94, 130, 0.3) 50%, rgba(58, 94, 130, 0.3) 60%,
      transparent 60%, transparent 70%,
      rgba(58, 94, 130, 0.3) 70%, rgba(58, 94, 130, 0.3) 80%,
      transparent 80%, transparent 90%,
      rgba(58, 94, 130, 0.3) 90%, rgba(58, 94, 130, 0.3) 100%
    );
  }
}

.cropper-control-row {
  margin-top: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f8f9fa;
  padding: 15px;
  border-radius: 8px;
  position: relative;
  
  .control-btn {
    border-color: #dcdfe6;
    background-color: #fff;
    border-radius: 6px;
    transition: all 0.3s;
    
    &:hover {
      background-color: #ecf5ff;
      color: #3a5e82;
      border-color: #c2d7eb;
    }
  }
  
  .select-btn {
    background: linear-gradient(135deg, #ecf5ff 0%, #f5f9ff 100%);
    border-color: #c2d7eb;
    
    &:hover {
      background: linear-gradient(135deg, #dbeaff 0%, #ecf5ff 100%);
      box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
    }
  }
  
  .submit-btn {
    background: linear-gradient(135deg, #3a5e82 0%, #4682b4 100%);
    border: none;
    box-shadow: 0 3px 8px rgba(58, 94, 130, 0.3);
    border-radius: 6px;
    transition: all 0.3s;
    
    &:hover {
      box-shadow: 0 5px 12px rgba(58, 94, 130, 0.4);
      transform: translateY(-2px);
    }
    
    &:active {
      transform: translateY(1px);
      box-shadow: 0 2px 6px rgba(58, 94, 130, 0.3);
    }
  }
}

.dialog-track {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 20px;
  overflow: hidden;
  
  .track-line {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 4px;
    background-image: repeating-linear-gradient(90deg, 
      transparent, transparent 10px, 
      #3a5e82 10px, #3a5e82 15px);
    opacity: 0.3;
    animation: trackMove 20s linear infinite;
  }
  
  .track-train {
    position: absolute;
    bottom: 6px;
    left: -40px;
    width: 30px;
    height: 10px;
    background: linear-gradient(135deg, #3a5e82 0%, #4682b4 100%);
    border-radius: 4px 12px 12px 4px;
    animation: avatarTrainMove 15s linear infinite;
    z-index: 2;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    
    &:before {
      content: "";
      position: absolute;
      bottom: -3px;
      right: 5px;
      width: 18px;
      height: 4px;
      background-color: rgba(58, 94, 130, 0.2);
      border-radius: 2px;
    }
    
    &:after {
      content: "";
      position: absolute;
      top: 3px;
      left: 5px;
      width: 4px;
      height: 4px;
      background-color: rgba(255,255,255,0.7);
      border-radius: 50%;
    }
  }
}

@keyframes trackMove {
  0% { transform: translateX(0); }
  100% { transform: translateX(50px); }
}

@keyframes avatarTrainMove {
  0% { left: -40px; opacity: 1; }
  85% { opacity: 1; left: calc(100% + 10px); }
  86% { opacity: 0; left: calc(100% + 10px); }
  87% { opacity: 0; left: -40px; }
  88% { opacity: 1; left: -40px; }
  100% { left: calc(20% - 40px); }
}
</style>
